﻿using System;
using System.Management;
using ClusterAwareProperties.Properties;

namespace ClusterAwareProperties
{
    /// <summary>
    /// Wrapper Disk ManagementObject
    /// </summary>
    public class DiskProperty: IDisposable
    {
        #region private property

        private readonly ManagementObject _classInstance;

        #endregion

        #region ctor

        /// <summary>
        /// constuctor
        /// </summary>
        private DiskProperty()
        {
            Status = StatusResource.Unknown;
            Characteristics = CharacteristicsResource.Unknown;
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="queryobj">Disk or DiskAvailabledDisk ManagementObject instance</param>
        public DiskProperty(ManagementObject queryobj)
            : this()
        {
            var path = queryobj.ToString();
            if (path.IndexOf("\\root\\MSCluster:MSCluster_Disk") == -1)
            {
                if (path.IndexOf("\\root\\MSCluster:MSCluster_AvailableDisk") == -1)
                {
                    throw new ClusterManagerException(string.Format(Resources.Error_TypeClassInvalid, path, "\\root\\MSCluster:MSCluster_Disk or \\root\\MSCluster_AvailableDisk"));
                }
            }
            _classInstance = queryobj;
            this.Convert(queryobj);
        }

        #endregion

        #region Properties

        /// <summary>
        /// Return instance of ResourceGroup ManagementObject
        /// </summary>
        public ManagementObject Instance
        {
            get { return _classInstance; }
        }


        /// <summary>
        /// Provides a short textual description of the disk.
        /// </summary>
        public string Caption { get; internal set; }

        /// <summary>
        /// Provides the characteristics of the disk. For a description of these characteristics, see CLUSCTL_RESOURCE_GET_CHARACTERISTICS.
        /// </summary>
        public CharacteristicsResource Characteristics { get; set; }

        /// <summary>
        /// Provides a textual description of the disk.
        /// </summary>
        public string Description { get; internal set; }

        /// <summary>
        /// Provides access to the flags set for the disk. For a list of possible characteristics, see CLUSCTL_GROUP_GET_FLAGS.
        /// </summary>
        public UInt32 Flags { get;  set; }

        /// <summary>
        /// Provides the ID of the disk.
        /// </summary>
        public string Id { get; internal set; }

        /// <summary>
        /// Indicates when the disk was installed. A lack of a value does not indicate that the element is not installed.
        /// </summary>
        public DateTime? InstallDate { get; internal set; }

        /// <summary>
        /// Defines the label by which the disk is known.
        /// </summary>
        public string Name { get; internal set; }

        /// <summary>
        /// Provides the SCSI bus of the disk.
        /// </summary>
        public UInt32 ScsiBus { get; internal set; }

        /// <summary>
        /// Provides the SCSI LUN of the disk.
        /// </summary>
        public UInt32 ScsiLun { get; internal set; }

        /// <summary>
        /// Provides the SCSI port number of the disk.
        /// </summary>
        public UInt32 ScsiPort { get; internal set; }

        /// <summary>
        ///Provides the SCSI target ID of the disk.
        /// </summary>
        public UInt32 ScsiTargetId { get; internal set; }

        /// <summary>
        /// Provides the signature of the disk.
        /// </summary>
        public UInt32 Signature { get; internal set; }

        /// <summary>
        /// Indicates the current status of the disk.
        /// </summary>
        public StatusResource Status { get; internal set; }

        #endregion

        #region IDisposable Members

        /// <summary>
        /// Dispose Class
        /// </summary>
        public void Dispose()
        {
            if (Instance != null)
            {
                Instance.Dispose();
            }
        }

        #endregion
    }
}
